"use strict";
/**
 * 根据行业编码和省份下载公司信息
 */

const url = require('url');
const querystring = require('querystring');
const utility = require('./utility');
const request = require('request');
const path = require('path');
const fs = require('fs');
const config = require('./config');

/**
 * 构造获取公司信息的url
 */
var reqUrl = url.format({
    protocol: 'http',
    host: 'www.neeq.com.cn',
    pathname: 'nqxxController/nqxx.do',
    search: querystring.stringify({
        callback: 'callFun',
        page: 0,
        typejb: 'T',
        xxzqdm: '',
        xxzrlx: '',
        xxhyzl: 'I65',
        xxssdq: encodeURI('广西省'),
        sortfield: 'xxzqdm',
        sorttype: 'asc',
        dicXxzbqs: '',
        xxfcbj: ''
    })
});

/**
 * 构造获取公司信息的url
 */
var companyInfoUrl = (code, province) => {
    return url.format({
        protocol: 'http',
        host: 'www.neeq.com.cn',
        pathname: 'nqxxController/nqxx.do',
        search: querystring.stringify({
            callback: 'callFun',
            page: 0,
            typejb: 'T',
            xxzqdm: '',
            xxzrlx: '',
            xxhyzl: code,
            xxssdq: encodeURI(province),
            sortfield: 'xxzqdm',
            sorttype: 'asc',
            dicXxzbqs: '',
            xxfcbj: ''
        })
    });
};

var requestInfo = (code, province, callback) => {
    let url = companyInfoUrl(code, province);
    request(url, (error, response, body) => {
        if (!error && response.statusCode == 200) {
            var obj = utility.parseResponseBody(body);

            if (obj.totalElements < 1) {
                console.error("没有数据");
                return;
            }

            if (callback) {
                callback(obj.content);
            }
        }
    });
};

module.exports.requestInfo = requestInfo;